﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using DBLGen.UI.Design.Query.DBAdapter;

namespace DBLGen
{
	[Editor(typeof(UI.Design.CustomSqlEditor), typeof(System.Drawing.Design.UITypeEditor))]
	public sealed class CustomAttributesQuery : MethodCollection
	{
		private Table _table;
		private List<Column> _items = new List<Column>();
		public string SQLDisplay = string.Empty;
		private List<QueryRow> queryRows = new List<QueryRow>();

		public void SetQueryRows(List<QueryRow> rows)
		{
			this.queryRows.Clear();
			this.queryRows.AddRange(rows);
		}

		public List<QueryRow> GetQueryRows()
		{
			return this.queryRows;
		}

		public CustomAttributesQuery()
		{ }

		public CustomAttributesQuery(Table table)
		{
			this._table = table;
		}

		public void SetOwner(Table owner)
		{
			if(owner == null)
				throw new ArgumentNullException("owner");
			if (this._table == null)
				this._table = owner;
		}

		public bool NeedToSetOwner()
		{
			return this._table == null ? true : false;
		}

		public CustomAttributesQuery Clone()
		{
			CustomAttributesQuery caq = new CustomAttributesQuery(this._table);
			caq.SQLDisplay = this.SQLDisplay;
			caq.SetQueryRows(this.queryRows);
			caq._items = this._items;
			caq._table = this._table;
			return caq;
		}

		public Table GetTable()
		{
			return _table;
		}

		public string GetDbConnectionString()
		{
			string result = @"Data Source={0};Initial Catalog={1};Persist Security Info={2};User ID={3};Password={4}";
			SettingsManager sm = _table.Project.SettingsManager;
			if (sm != null)
			{
				SettingsManager.SettingsBag smb = sm.Items[1];
				if (smb != null)
				{
					NameValueCollection col = smb.Settings;
					string datasource = col[0].ToString();
					string username = col[1].ToString();
					string password = col[2].ToString();
					string initCatalog = col[3].ToString();
					bool persistSecurityInfo = bool.Parse(col[4].ToString());
					result = string.Format(
							result,
							datasource,
							initCatalog,
							persistSecurityInfo.ToString(),
							username,
							password
						);

				}
			}

			return string.Format(result);
		}

		public override string ToString()
		{
			return queryRows.Count + " Parameters defined";
		}

		#region Xml Persistence

		internal static CustomAttributesQuery LoadXml(XPathNavigator navigator)
		{

			CustomAttributesQuery ca = new CustomAttributesQuery();
			
			XPathNodeIterator ni = navigator.Select("QueryRow/CustomAttributeQueryType");
			if (ni == null)
				return null;

			while (ni.MoveNext())
			{				
				XPathNavigator current = ni.Current;
				QueryRow row = QueryRow.LoadFromXml(current);
				ca.queryRows.Add(row);
			}

			return ca;
		}

		internal void SaveXml(XmlWriter writer)
		{
			if (this.queryRows.Count > 0)
			{
				writer.WriteStartElement("QueryRow");
				foreach (QueryRow item in this.queryRows)
				{
					item.SaveXml(writer);
				}
				writer.WriteEndElement();
			}
		}
		#endregion
	}
}
